import { PrismaClient, Prisma } from "@prisma/client";
const prisma = new PrismaClient();

async function main() {
  console.log(`Start seeding ...`);

  // 插入用户数据
  const userData = {
    name: 'test2',
    email: 'test2@google.com',
    password: '$2a$10$bv9RbmTnwEAXw8Z3pb/rQOiNfNorNS2jkcEsqatAp0IQRGB7PSFge',
    role: 'ADMIN',
    isTwoFactorEnabled: false
  };
  await createUser(userData);

  // 查找新插入的用户
  const user = await prisma.user.findFirst({
    where: { email: "test2@google.com" },
  });
  console.log(user);

  // 插入任务数据
  if (user) {
    await createTask({
      name: "task2",
      syncStrategy: "false",
      status: "pending",
      platform: ["jindong", "douyinshangchen"],
      keywords: "RTX 3060",
      userId: user.id, // 使用插入用户后的 ID
    });
  }

  // 查找插入的任务
  const task = await prisma.task.findFirst({
    where: { name: "task2" },
  });
  if (task) {
    // 插入商品数据
    await createGoods({
      sku_name: "【全新盒装】GTX1050TI 4G 百战-干将",
      shop_name: "万竞显卡旗舰店",
      sku_id:"1008611",
      source:"JD",
      final_price: "1399",
      jd_price: "1399",
      detail: "RTX 3060",
      update_time:new Date().toISOString(),
      taskId: task.id, // 使用插入任务后的 ID
    });
  }

  console.log(`Seeding finished.`);
}

// 创建用户
async function createUser(userData: any) {
  try {
    const newUser = await prisma.user.create({
      data: userData,
    });
    console.log("Created user:", newUser);
  } catch (error) {
    console.error("Error creating user:", error);
  }
}

// 创建任务
async function createTask(taskData: any) {
  try {
    const newTask = await prisma.task.create({
      data: taskData,
    });
    console.log("Created task:", newTask);
  } catch (error) {
    console.error("Error creating task:", error);
  }
}

// 创建商品
async function createGoods(goodsData: any) {
  try {
    const newGoods = await prisma.goods.create({
      data: goodsData,
    });
    console.log("Created goods:", newGoods);
  } catch (error) {
    console.error("Error creating goods:", error);
  }
}

main()
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });
